---
title: "Gender_typicality_cleaning"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE,message=FALSE)

setwd("/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Sexuality and Gender Typicality 2024")

```


```{r, include=FALSE}
rm(list=ls())
```

```{r}
gt_national_1 <- read.csv("gt_national_1.csv")

```


```{r}
#Adding respondent ID
gt_national_1$respondent <- seq.int(nrow(gt_national_1))
```


```{r}
#Keeping only those respondents that Answered they could hear a sound

gt_national_1 <- subset(gt_national_1, Finished!="False")
#217 didn't (from 3396 to 3182)

gt_national_1 <- subset(gt_national_1, Response.Type!="No Sound")
#217 didn't (from 3182 to 2965)

gt_national_1 <- subset(gt_national_1, Response.Type!="Failed Audio Check")
#200 failed audio check (from 2965 to 2765)

gt_national_1<-subset(gt_national_1, Response.Type!="Failed Attention Check")
#794 failed attention check (from 2765 to 1971)

gt_national_2 <- gt_national_1

```

```{r}
library(dplyr)
gt_national_2<-gt_national_2 %>% 
  dplyr::rename(
    face_1 = face1, face_2 = face2, face_3 = face3, face_4 = face4,
    voice_1 = voice1,voice_2 = voice2,voice_3 = voice3,voice_4 = voice4)

gt_national_2<-gt_national_2 %>% 
  dplyr::rename(feeling_1 = Q34_1, feeling_2 = Q87_1, feeling_3 = Q80_1, feeling_4 = Q62_1,
    support_1 = Q24, support_2 = Q88, support_3 = Q81, support_4 = Q63)

```


```{r}
# Subset to turn into long format -- only those variables that need to be turned into long format initially -- DVs (feeling + support) & face + voice + respondent ofc

df_national = subset(gt_national_2, select = c(respondent,feeling_1,feeling_2,feeling_3,feeling_4,
                             support_1, support_2, support_3,support_4, face_1,face_2,face_3,face_4, voice_1,voice_2,voice_3,voice_4))

df_national <- df_national %>%
  mutate(
    respondent = as.character(respondent),
    feeling_1 = as.character(feeling_1),
    feeling_2 = as.character(feeling_2),
    feeling_3 = as.character(feeling_3),
    feeling_4 = as.character(feeling_4),
    support_1 = as.character(support_1),
    support_2 = as.character(support_2),
    support_3 = as.character(support_3),
    support_4 = as.character(support_4),
    face_1 = as.character(face_1),
    face_2 = as.character(face_2),
    face_3 = as.character(face_3),
    face_4 = as.character(face_4),
    voice_1 = as.character(voice_1),
    voice_2 = as.character(voice_2),
    voice_3 = as.character(voice_3),
    voice_4 = as.character(voice_4)
  )


```


```{r}
library(dplyr)
library(tidyr)
gt_long_national <- df_national %>% 
  pivot_longer(-respondent, names_sep = '_', names_to = c('variable', 'candidate')) %>%
  pivot_wider(names_from = 'variable', values_from = 'value')
```

```{r}
# Create a dummy variable 'v' indicating whether the gender typicality / conformity (gt) manipulation 
# was made through voice (as opposed to face)

gt_long_national$v <- ifelse(gt_long_national$candidate == 1 | gt_long_national$candidate == 2, 0, 1)
gt_long_national$feeling<-as.numeric(gt_long_national$feeling)
```

```{r}
#Create a sexual orientation variable
library(stringr)
so_national<-as.data.frame(str_split_fixed(gt_long_national$voice,"_",n=2))

gt_long_national<-cbind(gt_long_national,so_national)
```

```{r}

gt_long_national$V2<-recode(gt_long_national$V2, "0"="mass","1"="neutral","2"="fem")
  
gt_long_national$V2[gt_long_national$v==0]<-gt_long_national$face[gt_long_national$v==0]
```

```{r}
gt2_national = subset(gt_national_2, select = -c(feeling_1,feeling_2,feeling_3,feeling_4, support_1, support_2,
                           support_3,support_4, face_1,face_2,face_3,face_4, voice_1,voice_2,voice_3,voice_4))

```

```{r}
library(dplyr)

# Ensure both respondent IDs are character
gt_long_national <- gt_long_national %>%
  mutate(respondent = as.character(respondent))

gt2_national <- gt2_national %>%
  mutate(respondent = as.character(respondent))

# Merge
gt_merged_national <- inner_join(gt_long_national, gt2_national, by = "respondent")


```

# Drop
```{r dropping & re-naming}
#Dropping & Re-naming
library(dplyr)
gt_merged_national = subset(gt_merged_national, select = -c(face,voice))


gt_merged_national<-gt_merged_national %>% 
  dplyr::rename(
    voice = v,
    so = V1,
    gt=V2
    )

```

```{r}
gt_merged_national$Democrat <- NA

gt_merged_national$Democrat <- ifelse(gt_merged_national$Q6 == "Democrat" | gt_merged_national$Q10 == "Closer to Democratic", 1, 0)

gt_merged_national$Republican <- NA

gt_merged_national$Republican <- ifelse(gt_merged_national$Q6 == "Republican" | gt_merged_national$Q10 == "Closer to Republican", 1, 0)



```

#Create Index for Homophobia & NOC
```{r}

#1st Recode Relevant Variables from Likert to Numeric (old new)

gt_merged_national$Q93 <-recode(gt_merged_national$Q93, "Strongly disagree"=1, 
                         "Moderately disagree"=2, "Slightly disagree"=3,
                         "Slightly agree"=4, "Moderately agree"=5, 
                         "Strongly agree"=6)

gt_merged_national$Q95 <-recode(gt_merged_national$Q95, "Strongly disagree"=1, 
                         "Moderately disagree"=2, "Slightly disagree"=3,
                         "Slightly agree"=4, "Moderately agree"=5, 
                         "Strongly agree"=6)

gt_merged_national$Q96 <-recode(gt_merged_national$Q96, "Strongly disagree"=1, 
                         "Moderately disagree"=2, "Slightly disagree"=3,
                         "Slightly agree"=4, "Moderately agree"=5, 
                         "Strongly agree"=6)

gt_merged_national$Q97 <-recode(gt_merged_national$Q97, "Strongly disagree"=1, 
                         "Moderately disagree"=2, "Slightly disagree"=3,
                         "Slightly agree"=4, "Moderately agree"=5, 
                         "Strongly agree"=6)

gt_merged_national$Q99 <-recode(gt_merged_national$Q99, "Strongly disagree"=1, 
                         "Moderately disagree"=2, "Slightly disagree"=3,
                         "Slightly agree"=4, "Moderately agree"=5, 
                         "Strongly agree"=6)

gt_merged_national$Q100 <-recode(gt_merged_national$Q100, "Strongly disagree"=1, 
                         "Moderately disagree"=2, "Slightly disagree"=3,
                         "Slightly agree"=4, "Moderately agree"=5, 
                         "Strongly agree"=6)

# Unreplaced values treated as NA as .x is not compatible. Please specify 
# replacements exhaustively or supply .default

gt_merged_national$orcert.scale <-apply(gt_merged_national[,c("Q93","Q95","Q96","Q97","Q99","Q100")],1,mean,na.rm=TRUE)


gt_merged_national$order_scale <-apply(gt_merged_national[,c("Q93","Q95","Q96")],1,mean,na.rm=TRUE)

gt_merged_national$certainty_scale <-apply(gt_merged_national[,c("Q97","Q99","Q100")],1,mean,na.rm=TRUE)

gt_merged_national <- gt_merged_national %>% 
  dplyr::mutate(orcert.quant3 = as.factor(ntile(orcert.scale,3)))


```

```{r index homophobia}

gt_merged_national$Q88_h <-recode(gt_merged_national$Q88_h, "Strongly disagree"=1, 
                         "Moderately disagree"=2, "Slightly disagree"=3,
                         "Slightly agree"=4, "Moderately agree"=5, 
                         "Strongly agree"=6)

gt_merged_national$Q90 <-recode(gt_merged_national$Q90, "Strongly disagree"=1, 
                         "Moderately disagree"=2, "Slightly disagree"=3,
                         "Slightly agree"=4, "Moderately agree"=5, 
                         "Strongly agree"=6)

gt_merged_national$Q91 <-recode(gt_merged_national$Q91, "Strongly disagree"=1, 
                         "Moderately disagree"=2, "Slightly disagree"=3,
                         "Slightly agree"=4, "Moderately agree"=5, 
                         "Strongly agree"=6)


## Remedy reverse coding absolute value
gt_merged_national$Q88_h <- abs(gt_merged_national$Q88_h-7)
gt_merged_national$Q90 <- abs(gt_merged_national$Q90-7)

gt_merged_national$homophobia.scale <- apply(gt_merged_national[,c("Q88_h","Q90","Q91")],1,mean,na.rm=TRUE)


```



#PARTISANSHIP
```{r}

# Make a variable named partyid_7 
gt_merged_national$partyid_7 <-NA

unique(gt_merged_national$Q10)
# Strong Democrat
gt_merged_national$partyid_7[gt_merged_national$Q6 == "Democrat" & gt_merged_national$Q9 == "Strong"] <- 1

# Not Very Strong Democrat
gt_merged_national$partyid_7[gt_merged_national$Q6 == "Democrat" & gt_merged_national$Q9 == "Not very strong"] <- 2

# Lean Democrat
gt_merged_national$partyid_7[gt_merged_national$Q6 == "Independent" | gt_merged_national$Q6 == "No Preference" | gt_merged_national$Q6 == "Other party (specify)"] <- 3

gt_merged_national$partyid_7[gt_merged_national$partyid_7 == 3 & gt_merged_national$Q10 == "Closer to Democratic"] <- 3

# Pure Independents
gt_merged_national$partyid_7[gt_merged_national$partyid_7 == 3 & gt_merged_national$Q10 == "Neither"] <- 4

# Lean Republican
gt_merged_national$partyid_7[gt_merged_national$partyid_7 == 3 & gt_merged_national$Q10 == "Closer to Republican"] <- 5

# Not Very Strong Republican
gt_merged_national$partyid_7[gt_merged_national$Q6 == "Republican" & gt_merged_national$Q8 == "Not very strong"] <- 6

# Strong Republican
gt_merged_national$partyid_7[gt_merged_national$Q6 == "Republican" & gt_merged_national$Q8 == "Strong"] <- 7


#Democrat - 0; Republican - 1; Independents - 2;

gt_merged_national$partyid_3 <- NA

# Democrat
gt_merged_national$partyid_3[gt_merged_national$partyid_7 == 1 | gt_merged_national$partyid_7 == 2 | gt_merged_national$partyid_7 == 3] <- "Democrat"

# Republican
gt_merged_national$partyid_3[gt_merged_national$partyid_7 == 5 | gt_merged_national$partyid_7 == 6 | gt_merged_national$partyid_7 == 7] <- "Republican"

# Independent
gt_merged_national$partyid_3[gt_merged_national$partyid_7 == 4] <- "Independent"

gt_merged_national$partyid_2 <- NA
gt_merged_national$partyid_2[gt_merged_national$partyid_3 == "Democrat"] <- "Democrat"
gt_merged_national$partyid_2[gt_merged_national$partyid_3 == "Republican"] <- "Republican"

class(gt_merged_national$partyid_2)

gt_merged_national$partyid_2_f <- as.factor(gt_merged_national$partyid_2)
```


```{r releveling reference categories}
gt_merged_national$gt <- as.factor(gt_merged_national$gt)

gt_merged_national <- within(gt_merged_national, gt <- relevel(gt, ref = "mass"))
gt_merged_national$so<-as.factor(gt_merged_national$so)
gt_merged_national <- within(gt_merged_national, so <- relevel(so, ref = "straight"))
```



#Demographic Variables 

```{r}
#AGE
gt_merged_national$age <- gt_merged_national$Q3

gt_merged_national$age<-recode(gt_merged_national$age, "18 - 24"=1, 
                       "25 - 34"=2, "35 - 44"=3,"45 - 54"=4,"55 - 64"=5,"65 or older"=6)


gt_merged_national$age_3<-gt_merged_national$Q3
  
gt_merged_national$age_3<-recode(gt_merged_national$age_3, "18 - 24"=1, 
                       "25 - 34"=1, "35 - 44"=2,"45 - 54"=2,"55 - 64"=3,"65 or older"=3)

#SEX
gt_merged_national$sex <- gt_merged_national$Q1
gt_merged_national$sex <- as.factor(gt_merged_national$sex)

gt_merged_national$sex_b<-gt_merged_national$Q1
gt_merged_national$sex_b<-recode(gt_merged_national$sex_b, "Male"=0, 
                       "Female"=1, "Non-binary"=2, "Other (specify)"=2)

#EDUCATION 

gt_merged_national$edun<-recode(gt_merged_national$edu, "Less than high school degree"=1, 
                       "High school graduate (high school diploma including GED)"=2, "Some college but no degree"=3,"Associate degree (2-year)"=4,"Bachelor's degree (4-year)"=5,"Master's degree"=6,"Doctoral degree"=7,"Professional degree (JD, MD)"=8)

#EDUCATION SIMPLER

gt_merged_national$edu2<-recode(gt_merged_national$edu, "Less than high school degree"="Less than high school", 
                       "High school graduate (high school diploma including GED)"="High school only", "Some college but no degree"="Some college","Associate degree (2-year)"="Some college","Bachelor's degree (4-year)"="BA","Master's degree"="Graduate Degree","Doctoral degree"="Graduate Degree","Professional degree (JD, MD)"="Graduate Degree")

#EDUCATION 5

gt_merged_national$edu5<-recode(gt_merged_national$edu, "Less than high school degree"=1, 
                       "High school graduate (high school diploma including GED)"=2, "Some college but no degree"=3,"Associate degree (2-year)"=3,"Bachelor's degree (4-year)"=4,"Master's degree"=5,"Doctoral degree"=5,"Professional degree (JD, MD)"=5)

#UNIVERSITY
gt_merged_national$uni <- NA

gt_merged_national$uni <- ifelse(gt_merged_national$edun <5, 0, 1)

table(gt_merged_national$uni)

#RACE
unique(gt_merged_national$Q2)
gt_merged_national$race_5 <- gt_merged_national$Q2
gt_merged_national$race_5<-recode(gt_merged_national$race_5, "White, non-Hispanic"="White", 
                       "Black or African American, non-Hispanic"="Black", "Latino, Latinx, or Hispanic"="Latinx","American Indian, Native American, or Alaska Native"="Other","Asian"="Asian","Native Hawaiian or Pacific Islander"="Other","Other"="Other")

gt_merged_national$race_n <- gt_merged_national$Q2
gt_merged_national$race_n<-recode(gt_merged_national$race_n, "White, non-Hispanic"=1, 
                       "Black or African American, non-Hispanic"=2, "Latino, Latinx, or Hispanic"=3,"American Indian, Native American, or Alaska Native"=5,"Asian"=4,"Native Hawaiian or Pacific Islander"=5,"Other"=5)



```


```{r}
write.csv(gt_merged_national,"/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Sexuality and Gender Typicality 2024/gt_national_working.csv", row.names = FALSE)

```



#PSSP

```{r}
setwd("/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Sexuality and Gender Typicality 2024")

gt_1_pssp <- read.csv("gt_pssp_1.csv")

```

```{r}
#Adding respondent ID
gt_1_pssp$respondent <- seq.int(nrow(gt_1_pssp))
```


```{r}

library(dplyr)
gt_1_pssp$Finished
#Keeping only those respondents that FINISHED the survey (i.e. those for whom data is available)
gt_2_pssp<-subset(gt_1_pssp, Finished!="False")

```

```{r excluding those that failed sound check}
library(dplyr)
gt_2_pssp <- gt_2_pssp %>%
  filter(Q185=="A cat meowing")

# 631-616 = 15 (15 people failed a sound check)
```



```{r}
library(dplyr)
gt_2_pssp <- gt_2_pssp %>% 
  dplyr::rename(
    face_1 = face1,   face_2 = face2,   face_3 = face3,   face_4 = face4,
    face_5 = face5,   face_6 = face6,   face_7 = face7,   face_8 = face8,
    voice_1 = voice1, voice_2 = voice2, voice_3 = voice3, voice_4 = voice4,
    voice_5 = voice5, voice_6 = voice6, voice_7 = voice7, voice_8 = voice8
  )


gt_2_pssp<-gt_2_pssp %>% 
  dplyr::rename(feeling_1 = Q34_1, feeling_2 = Q30_1, feeling_3 = Q44_1, feeling_4 = Q53_1,
                feeling_5 = Q62_1, feeling_6 = Q71_1, feeling_7 = Q80_1, feeling_8 = Q87_1,
                support_1 = Q24, support_2 = Q29, support_3 = Q45, support_4 = Q54,
                support_5 = Q63, support_6 = Q72, support_7 = Q81, support_8 = Q88)

```



```{r}
# Subset to turn into long format -- only those variables that need to be turned into long format

df_pssp = subset(gt_2_pssp, select = c(respondent,feeling_1,feeling_2,feeling_3,feeling_4,feeling_5,feeling_6,
                           feeling_7,feeling_8, support_1, support_2,
                           support_3,support_4,support_5,support_6,support_7, support_8, face_1,face_2,face_3,face_4,face_5,face_6,face_7,face_8, voice_1,voice_2,voice_3,voice_4,voice_5,voice_6,voice_7, voice_8))

```

```{r}
library(tidyr)
library(dplyr)

gt_long_pssp <- df_pssp %>%
  pivot_longer(
    cols = -respondent,
    names_to = c(".value", "candidate"),
    names_sep = "_"
  )

```


```{r}
#create a dummy -- if we manipulate gt through voice (candidates 4,5,6,7), v takes value of 1 if not (candidates 1,2,3,8) v takes value of 0

gt_long_pssp$v <- ifelse(gt_long_pssp$candidate == 8 | gt_long_pssp$candidate < 4, 0, 1)
gt_long_pssp$feeling<-as.numeric(gt_long_pssp$feeling)
```

```{r}
#creating a sexual orientation variable
library(stringr)
so_pssp<-as.data.frame(str_split_fixed(gt_long_pssp$voice,"_",n=2))

gt_long_pssp<-cbind(gt_long_pssp,so_pssp)
```

```{r}

gt_long_pssp$V2<-recode(gt_long_pssp$V2, "0"="mass","1"="neutral","2"="fem")
  
gt_long_pssp$V2[gt_long_pssp$v==0]<-gt_long_pssp$face[gt_long_pssp$v==0]
```



```{r}
#Removing all these variables
gt2_pssp = subset(gt_2_pssp, select = -c(feeling_1,feeling_2,feeling_3,feeling_4,feeling_5,feeling_6,
                           feeling_7,feeling_8, support_1, support_2,
                           support_3,support_4,support_5,support_6,support_7, support_8, face_1,face_2,face_3,face_4,face_5,face_6,face_7,face_8, voice_1,voice_2,voice_3,voice_4,voice_5,voice_6,voice_7, voice_8))

ls(gt2_pssp)

```


```{r}
#Merging
library(dplyr)
gt_merged_pssp <- as.data.frame(dplyr::inner_join(gt_long_pssp, gt2_pssp, by = "respondent"))

```

# Drop
```{r dropping & re-naming}
#Dropping & Re-naming
library(dplyr)
gt_merged_pssp = subset(gt_merged_pssp, select = -c(face,voice))


gt_merged_pssp<-gt_merged_pssp %>% 
  dplyr::rename(
    voice = v,
    so = V1,
    gt=V2
    )

```

```{r party ID variable creation}

partyid_pssp <-as.data.frame(str_split_fixed(gt_merged_pssp$Q10,"Closer to ",n=2))

gt_merged_pssp<-cbind(gt_merged_pssp,partyid_pssp)

gt_merged_pssp$V2<-as.factor(gt_merged_pssp$V2)

gt_merged_pssp$V2[gt_merged_pssp$Q6=="Democrat"]<-gt_merged_pssp$partisanship[gt_merged_pssp$Q6=="Democrat"]
gt_merged_pssp$V2[gt_merged_pssp$Q6=="Republican"]<-gt_merged_pssp$partisanship[gt_merged_pssp$Q6=="Republican"]
# table(gt_merged_pssp$V2)/8

gt_merged_pssp<-gt_merged_pssp %>% 
  dplyr::rename(
    party.id = V2)

class(gt_merged_pssp$party.id)
table(gt_merged_pssp$party.id)
gt_merged_pssp <- within(gt_merged_pssp, party.id <- relevel(party.id, ref = "Democratic"))

```



#Create Index for Homophobia & NOC
```{r}

#1st Recode Relevant Variables from Likert to Numeric (old new)

gt_merged_pssp$Q15_1<-recode(gt_merged_pssp$Q15_1, "Strongly Disagree"=1, 
                         "Disagree"=2, "Don't Know"=3,"Agree"=4,
                         "Strongly Agree"=5)

gt_merged_pssp$Q15_2<-recode(gt_merged_pssp$Q15_2, "Strongly Disagree"=1, 
                         "Disagree"=2, "Don't Know"=3,"Agree"=4,
                         "Strongly Agree"=5)

gt_merged_pssp$Q15_3<-recode(gt_merged_pssp$Q15_3, "Strongly Disagree"=1, 
                         "Disagree"=2, "Don't Know"=3,"Agree"=4,
                         "Strongly Agree"=5)

gt_merged_pssp$Q15_4<-recode(gt_merged_pssp$Q15_4, "Strongly Disagree"=1, 
                         "Disagree"=2, "Don't Know"=3,"Agree"=4,
                         "Strongly Agree"=5)

```



```{r index homophobia}
## Remedy reverse coding absolute value (original - max)
gt_merged_pssp$Q15_1 <- abs(gt_merged_pssp$Q15_1-6)
gt_merged_pssp$Q15_2 <- abs(gt_merged_pssp$Q15_2-6)

gt_merged_pssp$homophobia.scale <- apply(gt_merged_pssp[,c("Q15_1","Q15_2","Q15_3","Q15_4")],1,mean,na.rm=TRUE)



```


```{r}
##Recording for the NFC Scale


gt_merged_pssp$Q14_1<-recode(gt_merged_pssp$Q14_1, "Strongly disagree"=1, 
                         "Moderately Disagree"=2, "Slightly Disagree"=3,
                         "Slightly Agree"=4, "Moderately Agree"=5, 
                         "Strongly Agree"=6)

gt_merged_pssp$Q14_2<-recode(gt_merged_pssp$Q14_2, "Strongly disagree"=1, 
                         "Moderately Disagree"=2, "Slightly Disagree"=3,
                         "Slightly Agree"=4, "Moderately Agree"=5, 
                         "Strongly Agree"=6)

gt_merged_pssp$Q14_3<-recode(gt_merged_pssp$Q14_3, "Strongly disagree"=1, 
                         "Moderately Disagree"=2, "Slightly Disagree"=3,
                         "Slightly Agree"=4, "Moderately Agree"=5, 
                         "Strongly Agree"=6)

gt_merged_pssp$Q14_7<-recode(gt_merged_pssp$Q14_7, "Strongly disagree"=1, 
                         "Moderately Disagree"=2, "Slightly Disagree"=3,
                         "Slightly Agree"=4, "Moderately Agree"=5, 
                         "Strongly Agree"=6)

gt_merged_pssp$Q14_8<-recode(gt_merged_pssp$Q14_8, "Strongly disagree"=1, 
                         "Moderately Disagree"=2, "Slightly Disagree"=3,
                         "Slightly Agree"=4, "Moderately Agree"=5, 
                         "Strongly Agree"=6)

gt_merged_pssp$Q14_9<-recode(gt_merged_pssp$Q14_9, "Strongly disagree"=1, 
                         "Moderately Disagree"=2, "Slightly Disagree"=3,
                         "Slightly Agree"=4, "Moderately Agree"=5, 
                         "Strongly Agree"=6)


```


```{r index NOC scales}
gt_merged_pssp$nfc.scale <- apply(gt_merged_pssp[,c("Q14_1","Q14_2","Q14_3","Q14_4","Q14_5","Q14_6","Q14_7",
                                            "Q14_8","Q14_9","Q14_10")],1,mean,na.rm=TRUE)

gt_merged_pssp$noc.scale <-apply(gt_merged_pssp[,c("Q14_1","Q14_2","Q14_3","Q14_7","Q14_8","Q14_9")],1,mean,na.rm=TRUE)

```


#PARTISANSHIP
```{r}

# Make a variable named partyid_7 
gt_merged_pssp$partyid_7 <-NA

# Strong Democrat
gt_merged_pssp$partyid_7[gt_merged_pssp$Q6 == "Democrat" & gt_merged_pssp$Q9 == "Strong"] <- 1

# Not Very Strong Democrat
gt_merged_pssp$partyid_7[gt_merged_pssp$Q6 == "Democrat" & gt_merged_pssp$Q9 == "Not very strong"] <- 2

# Lean Democrat
gt_merged_pssp$partyid_7[gt_merged_pssp$Q6 == "Independent" | gt_merged_pssp$Q6 == "No Preference" | gt_merged_pssp$Q6 == "Other party (specify)"] <- 3

gt_merged_pssp$partyid_7[gt_merged_pssp$partyid_7 == 3 & gt_merged_pssp$Q10 == "Closer to Democratic"] <- 3

# Pure Independents
gt_merged_pssp$partyid_7[gt_merged_pssp$partyid_7 == 3 & gt_merged_pssp$Q10 == "Neither"] <- 4

# Lean Republican
gt_merged_pssp$partyid_7[gt_merged_pssp$partyid_7 == 3 & gt_merged_pssp$Q10 == "Closer to Republican"] <- 5

# Not Very Strong Republican
gt_merged_pssp$partyid_7[gt_merged_pssp$Q6 == "Republican" & gt_merged_pssp$Q8 == "Not very strong"] <- 6

# Strong Republican
gt_merged_pssp$partyid_7[gt_merged_pssp$Q6 == "Republican" & gt_merged_pssp$Q8 == "Strong"] <- 7


#Democrat - 0; Republican - 1; Independents - 2;

gt_merged_pssp$partyid_3 <- NA

# Democrat
gt_merged_pssp$partyid_3[gt_merged_pssp$partyid_7 == 1 | gt_merged_pssp$partyid_7 == 2 | gt_merged_pssp$partyid_7 == 3] <- "Democrat"

# Republican
gt_merged_pssp$partyid_3[gt_merged_pssp$partyid_7 == 5 | gt_merged_pssp$partyid_7 == 6 | gt_merged_pssp$partyid_7 == 7] <- "Republican"

# Independent
gt_merged_pssp$partyid_3[gt_merged_pssp$partyid_7 == 4] <- "Independent"

```



```{r releveling reference categories}
gt_merged_pssp$gt <- as.factor(gt_merged_pssp$gt)

gt_merged_pssp <- within(gt_merged_pssp, gt <- relevel(gt, ref = "mass"))
gt_merged_pssp$so<-as.factor(gt_merged_pssp$so)
gt_merged_pssp <- within(gt_merged_pssp, so <- relevel(so, ref = "straight"))

```

#Demographic Variables 

```{r}
#AGE
gt_merged_pssp$age_3 <- gt_merged_pssp$Q3

gt_merged_pssp$age_3<-recode(gt_merged_pssp$age_3, "18-24"=1, 
                       "25-34"=2, "35-44"=3,"45 or older"=3)

#SEX
gt_merged_pssp$sex <- gt_merged_pssp$Q1
gt_merged_national$sex <- as.factor(gt_merged_national$sex)

gt_merged_pssp$sex_b<-gt_merged_pssp$Q1
gt_merged_pssp$sex_b<-recode(gt_merged_pssp$sex_b, "Male"=0, 
                       "Female"=1, "Other (specify)"=2)

#RACE
unique(gt_merged_pssp$Q2)
gt_merged_pssp$race_5 <- gt_merged_pssp$Q2
gt_merged_pssp$race_5<-recode(gt_merged_pssp$race_5, "White, non-Hispanic"="White", 
                       "Black or African American, non-Hispanic"="Black", "Latino, Latinx, or Hispanic"="Latinx","American Indian, Native American, or Alaska Native"="Other","Asian"="Asian","Native Hawaiian or Pacific Islander"="Other","Other"="Other")


```



```{r}
write.csv(gt_merged_pssp,"/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Sexuality and Gender Typicality 2024/gt_pssp_working.csv", row.names = FALSE)


```


# PSSP Manipulation Check

```{r}
gt_manipulation_m <- gt_2_pssp
```


```{r renaming}
gt_manipulation_m<-gt_manipulation_m %>% 
  dplyr::rename(
    masculinity_1 = Q123_1,
    masculinity_2 = Q132_1,
    masculinity_3=Q127_1,
     masculinity_4=Q169_1, masculinity_5=Q162_1, masculinity_6=Q164_1, masculinity_7=Q167_1,
     masculinity_8=Q136_1,
    )

gt_manipulation_m<-gt_manipulation_m %>% 
  dplyr::rename(
    gt_1 = final_face1,
    gt_2 = final_face6,
    gt_3=final_face3,
     gt_4=final_voice8, gt_5=final_voice1, gt_6=final_voice4, gt_7=final_voice6,
     gt_8=final_face2,
    )

```

```{r subsetting gt_manipulation_m}

# Subset to turn into long format -- only those variables that need to be turned into long format initially

manipulation_long = subset(gt_manipulation_m, 
                           select = c(respondent,masculinity_1,masculinity_2, masculinity_3,
                                      masculinity_4,masculinity_5, 
                                      masculinity_6,masculinity_7,masculinity_8,
                                      gt_1,gt_2,gt_3,gt_4,gt_5,gt_6,gt_7,gt_8))

```

```{r}
manipulation_long <- manipulation_long %>%
  mutate(across(starts_with("gt_"), as.character))

```

```{r}
library(dplyr)
library(tidyr)

manipulation_long1 <- manipulation_long %>% 
  pivot_longer(
    cols = -respondent,
    names_to = c(".value", "candidate"),
    names_sep = "_"
  )

```


```{r}

manipulation_long1$masculinity<-as.numeric(manipulation_long1$masculinity)
manipulation_long1$candidate<-as.numeric(manipulation_long1$candidate)

manipulation_long1$v <- 
  ifelse(manipulation_long1$candidate == 8 | manipulation_long1$candidate < 4, 0, 1)


manipulation_long1$gt<-recode(manipulation_long1$gt, "0"="mass","1"="neutral","2"="fem")

```


```{r}
write.csv(manipulation_long1,"/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Sexuality and Gender Typicality 2024/pssp_mc.csv", row.names = FALSE)
```


